{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer,AutoModel\n",
    "# from thuglm.modeling_chatglm import ChatGLMForConditionalGeneration\n",
    "import torch\n",
    "from peft import get_peft_model, LoraConfig, TaskType\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Explicitly passing a `revision` is encouraged when loading a configuration with custom code to ensure no malicious code has been contributed in a newer revision.\n",
      "Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a4d91b662c8349759fed203f990954ca",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/8 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\yuanz\\anaconda3\\envs\\mynet\\lib\\site-packages\\peft\\tuners\\lora.py:173: UserWarning: fan_in_fan_out is set to True but the target module is not a Conv1D. Setting fan_in_fan_out to False.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "PeftModelForCausalLM(\n",
       "  (base_model): LoraModel(\n",
       "    (model): ChatGLMForConditionalGeneration(\n",
       "      (transformer): ChatGLMModel(\n",
       "        (word_embeddings): Embedding(150528, 4096)\n",
       "        (layers): ModuleList(\n",
       "          (0): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (1): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (2): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (3): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (4): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (5): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (6): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (7): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (8): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (9): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (10): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (11): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (12): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (13): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (14): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (15): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (16): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (17): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (18): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (19): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (20): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (21): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (22): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (23): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (24): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (25): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (26): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "          (27): GLMBlock(\n",
       "            (input_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (attention): SelfAttention(\n",
       "              (rotary_emb): RotaryEmbedding()\n",
       "              (query_key_value): MergedLinear(\n",
       "                in_features=4096, out_features=12288, bias=True\n",
       "                (lora_dropout): Dropout(p=0.1, inplace=False)\n",
       "                (lora_A): Linear(in_features=4096, out_features=16, bias=False)\n",
       "                (lora_B): Conv1d(16, 8192, kernel_size=(1,), stride=(1,), groups=2, bias=False)\n",
       "              )\n",
       "              (dense): Linear(in_features=4096, out_features=4096, bias=True)\n",
       "            )\n",
       "            (post_attention_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "            (mlp): GLU(\n",
       "              (dense_h_to_4h): Linear(in_features=4096, out_features=16384, bias=True)\n",
       "              (dense_4h_to_h): Linear(in_features=16384, out_features=4096, bias=True)\n",
       "            )\n",
       "          )\n",
       "        )\n",
       "        (final_layernorm): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n",
       "      )\n",
       "      (lm_head): Linear(in_features=4096, out_features=150528, bias=False)\n",
       "    )\n",
       "  )\n",
       ")"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = AutoModel.from_pretrained(\n",
    "    \"yuanzhoulvpi/chatglm6b-dddd\", trust_remote_code=True).half().cuda()\n",
    "\n",
    "peft_config = LoraConfig(\n",
    "    task_type=TaskType.CAUSAL_LM,\n",
    "    inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1,\n",
    "    target_modules=['query_key_value',],\n",
    ")\n",
    "model = get_peft_model(model, peft_config)\n",
    "\n",
    "# 在这里加载lora模型，注意修改chekpoint\n",
    "peft_path = \"test004/checkpoint-100/chatglm-lora.pt\"\n",
    "model.load_state_dict(torch.load(peft_path), strict=False)\n",
    "model.eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.\n"
     ]
    }
   ],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(\"yuanzhoulvpi/chatglm6b-dddd\", trust_remote_code=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "冰红茶和柠檬茶的味道并不完全相同,虽然它们都是茶饮料,但它们的味道是由不同的茶叶、添加剂和糖精制而成的。\n",
      "\n",
      "冰红茶是一种茶饮料,由红茶、糖精、水和其他添加剂制成。它的味道通常比柠檬茶更甜和浓郁。\n",
      "\n",
      "柠檬茶是一种混合了柠檬和茶的饮料,它的味道主要由柠檬汁、茶和糖精制成。由于柠檬和茶都含有大量的天然调味品,所以它们的味道相似。\n"
     ]
    }
   ],
   "source": [
    "text =\"为什么冰红茶和柠檬茶的味道一样？\"\n",
    "\n",
    "with torch.autocast(\"cuda\"):\n",
    "    res, history = model.chat(tokenizer=tokenizer, query=text,max_length=300)\n",
    "    print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mynet",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
